package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SessParty;
import com.crane.common.core.dto.SingleResponse;
import com.crane.oss.enums.GradeEnum;
import com.crane.oss.enums.SchoolSystemEnum;
import com.crane.oss.enums.SectionEnum;
import com.crane.oss.enums.UserTypeEnum;
import com.crane.oss.properties.SysConfigProperties;
import com.crane.oss.school.bean.request.area.UpdateSchoolTimeReq;
import com.crane.oss.sys.bean.request.party.*;
import com.crane.oss.sys.bean.resp.PartyBean;
import com.crane.oss.sys.bean.resp.TreePartyBean;
import com.crane.oss.sys.service.PartyService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2018年12月28日 19时42分58秒
 */

@Api(value = "PartyController", tags = {"机构信息"})
@RestController
@RequestMapping("/sys/party")
public class PartyController extends BaseBusinessController {

    @Autowired
    private PartyService partyService;

    @Autowired
    private SysConfigProperties sysConfigProperties;

    @ApiOperation(value = "获取机构列表信息", notes = "获取机构列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryPartyRequest")
//    @RequiresPermissions("sys:party:list")
    @PostMapping("/list")
    public MultiResponse list(@RequestBody QueryPartyRequest request) {
        // 查询列表数据
        SessParty sessParty = getParty();
        if (null != sessParty) {
            request.setParentId(sessParty.getPartyId());
        }
        IPage<PartyBean> page = partyService.queryList(request);
        return MultiResponse.of(page);
    }


    @ApiOperation(value = "新增市区机构", notes = "新增市区机构")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddPartyRequest")
//    @RequiresPermissions("sys:party:save")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddPartyRequest request) {
        SessParty sessParty = getSessParty();
        if (null == request.getParentId()) {
            if (!sessParty.getPartyId().equals(sysConfigProperties.getAdminPartyId())) {
                return Response.buildFailure("没有权限！");
            }
        }
        request.setCreateUserId(getUserId());
        request.setCreatePartyId(getPartyId());
        partyService.save(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改市区机构", notes = "修改市区机构")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdatePartyRequest")
//    @RequiresPermissions("sys:party:update")
    @PostMapping("/update")
    public Response save(@RequestBody @Valid UpdatePartyRequest request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        partyService.update(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除机构", notes = "删除机构")
//    @RequiresPermissions("sys:party:delete")
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        partyService.delete(Long.parseLong(sid), getPartyId());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "获取机构详情", notes = "获取机构详情")
//    @RequiresPermissions("sys:party:info")
    @PostMapping("/info")
    public SingleResponse info(@RequestBody Map<String,Long> data) {
        if(null == data.get("partyId")){
            return SingleResponse.buildFailure("机构信息未传入");
        }
        PartyBean partyBean = partyService.queryParty(data.get("partyId"));
        return SingleResponse.of(partyBean);
    }

    @ApiOperation(value = "获取机构菜单树信息", notes = "菜单管理菜单树")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryPartyTreeRequest")
//    @RequiresPermissions("sys:party:treeList")
    @PostMapping("/partyTreeList")
    public SingleResponse partyTreeList(@RequestBody QueryPartyTreeRequest request) {
        TreePartyBean treeBean = partyService.queryPartyTree(request, getPartyId());
        return SingleResponse.of(treeBean);
    }


    @ApiOperation(value = "新增学校机构", notes = "新增学校机构")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddSchoolPartyRequest")
//    @RequiresPermissions("sys:party:save")
    @PostMapping("/saveSchool")
    public Response saveSchool(@RequestBody @Valid AddSchoolPartyRequest request) {
        request.setCreateUserId(getUserId());
        request.setCreatePartyId(getPartyId());
        partyService.saveSchool(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改学校机构", notes = "修改学校机构")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateSchoolPartyRequest")
//    @RequiresPermissions("sys:party:update")
    @PostMapping("/updateSchool")
    public Response updateSchool(@RequestBody @Valid UpdateSchoolPartyRequest request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        partyService.updateSchool(request);
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改学校开学，毕业时间", notes = "修改学校开学，毕业时间")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateSchoolTimeReq")
//    @RequiresPermissions("sys:party:update")
    @PostMapping("/updateSchoolTime")
    public Response updateSchoolTime(@RequestBody @Valid UpdateSchoolTimeReq request) {
        request.setUpdateUserId(getUserId());
        request.setUpdatePartyId(getPartyId());
        partyService.updateSchoolTime(request);
        return Response.buildSuccess();
    }

}
